#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using System.IO;
using NUnit.Framework;
using BitwiseOre.SharedMemory;

namespace BitwiseOre.Tests.SharedMemory
{
    [TestFixture]
    public sealed class SharedMemoryContextFixture : AssertionHelper
    {
        [Test(Description = "Ensures that creating a listner with a null name throws an exception.")]
        public void CreateListener_WithNullName()
        {
            var context = new SharedMemoryContext();

            Expect(
                () => context.CreateListener(null),
                Throws.InstanceOf<ArgumentNullException>());
        }

        [Test(Description = "Checks that creating a listener returns a non-null value.")]
        public void CreateListener_WithName()
        {
            var context = new SharedMemoryContext();

            Expect(context.CreateListener("testListner"), Not.Null, 
                "CreateListner did not return a non-null listener.");
        }

        [Test(Description = "Checks that creating a factory return a non-null value.")]
        public void CreateFactory_WithName()
        {
            var context = new SharedMemoryContext();
            using(context.CreateListener("testName"))
            using(var factory = context.CreateFactory("testName"))
            {
                Expect(factory, Not.Null,
                "CreateFactory did not return a non-null factory.");
                
            }
        }

        [Test(Description = "Ensures that creating a listner with a name already in use throws an exception.")]
        public void CreateListner_WithInUseName()
        {
            var context = new SharedMemoryContext();
            using (context.CreateListener("testName"))
            {
                Expect(() => context.CreateListener("testName"), Throws.InstanceOf<IOException>(),
                       "Creating a listner with a name that is in used did not throw the expected exception.");
            }
        }

        [Test(Description = "Ensures that creating a factory with a null name throws an exception")]
        public void CreateFactory_WithNullName()
        {
            var context = new SharedMemoryContext();

            Expect(
                () => context.CreateFactory(null), 
                Throws.InstanceOf<ArgumentNullException>());
        }
    }
}